<template>
  <el-dialog :title="formData.id ? '编辑部门': '新增部门'" :visible="visible" @close="oncancel">
    <el-form ref="formRef" :model="formData" :rules="formRules">
      <el-form-item label="部门名称" label-width="100px" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="100px" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="100px" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择部门负责人" style="width:100%;">
          <!-- label是用来展示的 ， value 是服务端要拿到的 -->
          <el-option v-for="item in ManagerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="100px" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item style="text-align:center;">
        <el-button type="primary" @click="onsubmit">确定</el-button>
        <el-button type="danger" @click="oncancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartmentApi, getManagerListApi, addDepartmentApi, getDepartmentDetailApi, updateDepartmentApi } from '@/api/department'
export default {
  // 父传子，在子组件接收
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名字
        pid: ''// 部门父级部门id
      },
      formRules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              let dept = await getDepartmentApi()
              // 编辑表单的校验，需要将点击的自己排除在外
              if (this.formData.id) {
                dept = dept.filter(item => item.id !== this.formData.id)
              }
              if (dept.some(item => item.code === value)) {
                callback(new Error('该部门编码已经存在了~~~'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              let dept = await getDepartmentApi()
              // 编辑表单的校验，需要将点击的自己排除在外
              if (this.formData.id) {
                dept = dept.filter(item => item.id !== this.formData.id)
              }
              if (dept.some(item => item.name === value)) {
                callback(new Error('该部门名称已经存在了~~~'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      ManagerList: []
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async getManagerList() {
      const res = await getManagerListApi()
      this.ManagerList = res
    },
    // 获取编辑数据的请求
    async getDepartmentDetai(id) {
      const res = await getDepartmentDetailApi(id)
      this.formData = res
    //   console.log(res)
    },
    async onsubmit() {
      // 1. 整体校验
      await this.$refs.formRef.validate()
      // 2. 发起提交请求
      // 有新增和编辑区别
      if (this.formData.id) {
        // 编辑部门
        await updateDepartmentApi(this.formData)
      } else {
        await addDepartmentApi({ ...this.formData, pid: this.currentNodeId })
      }
      this.$message.success(`${this.formData.id ? '编辑' : '新增'}成功~~~`)
      // 3. 通知父组件更新
      this.$emit('updateDepartment')
      // 4. 重置表单 + 关闭弹层
      this.oncancel()
    },
    oncancel() {
      // 重置表单
      this.$refs.formRef.resetFields()
      // 关闭弹层
      this.$emit('update:visible', false)
      // 这里是因为，编辑部门是由id，但是关闭弹层为清理，我们需要清理id等数据
      this.formData = {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        managerId: '', // 部门负责人名字
        name: '', // 部门名字
        pid: ''// 部门父级部门id
      }
    }
  }
}
</script>

  <style lang="scss" scoped></style>
